import 'dart:async';
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/extensions.dart';

class DiceComponent extends SpriteAnimationComponent with HasGameRef {
  Vector2 diceSize;
  Vector2 dicePosition;
  int stopIndex;

  DiceComponent(
      {required this.diceSize, required this.dicePosition, this.stopIndex = 0})
      : super(position: dicePosition, size: diceSize, anchor: Anchor.center);

  @override
  FutureOr<void> onLoad() async {
    List<Sprite> sprites = [];
    for (int i = 1; i <= 9; i++) {
      sprites.add(await gameRef.loadSprite('mahjong/dice/dice$i.png'));
    }
    animation = SpriteAnimation.spriteList(sprites, stepTime: 0.15, loop: true);

    add(TimerComponent(
        period: 1,
        onTick: () {
          animation = SpriteAnimation.spriteList(
            [sprites[stopIndex - 1]],
            stepTime: 0.15,
            loop: false,
          );
        }));

    super.onLoad();
  }

  @override
  void render(Canvas canvas) {
    super.render(canvas);
  }
}
